/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * fastAddSpravka_Item.java
 *
 * Created on Jan 25, 2009, 11:37:56 PM
 */

package GUI.other;

import GUI.AddToSklad;
import functions.Breeds;
import functions.Distributors;
import functions.Dogy;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.sql.SQLException;

import javax.swing.JOptionPane;
import model.Breed;
import model.Distributor;
import model.Drug;
import model.Item;

/**
 *
 * @author JArik
 */
public class fastBreedAdd extends fastAdd {

    /** Creates new form fastAddSpravka_Item */
    private Breed toRec;
    private fastAddInterface parent;
    private String itemText, breedName;
    public fastBreedAdd(String breedN,fastAddInterface nParent) {
        super(nParent);
        initComponents();
        parent = nParent;
        breedName = breedN;

        itemText = "Название";
        if(!parent.getActiveFieldText().equals(""))
            breedNameField.setText(parent.getActiveFieldText());
        else
        {
          breedNameField.setText(itemText);
          breedNameField.setForeground(Color.gray);
        }

       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        breedNameField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true));

        jLabel1.setText("Быстрое добавление в каталог");

        breedNameField.setText("Name");
        breedNameField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                breedNameFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                breedNameFieldFocusLost(evt);
            }
        });
        breedNameField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                breedNameFieldKeyReleased(evt);
            }
        });

        jButton1.setText("Закрыть");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Добавить");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(breedNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(breedNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        parent.disposeDialog();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       try
       {
         if(breedNameField.getText().equals(itemText) || breedNameField.getText().equals(""))
             throw new Exception("Введите название поставщика.");
         Breed brd = new Breed();
         brd.setName(breedNameField.getText());

         int id=-1;
         //if( parent.getChoosenItem() instanceof Drug)
            id  = Breeds.addToCatalog(brd);
         //else if(parent.getChoosenItem() instanceof Item)
         //    id = Distributors.addOtherToCatalog(distr);
         brd.setId(id);
         toRec = brd;
         parent.setActiveFieldText(brd);
       } // try
       catch(Exception e)
       {
           JOptionPane.showMessageDialog(null, e.getMessage());
       }
       parent.disposeDialog();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void breedNameFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_breedNameFieldFocusGained
       Dogy.focusGained(breedNameField, itemText);
}//GEN-LAST:event_breedNameFieldFocusGained

    private void breedNameFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_breedNameFieldFocusLost
        Dogy.focusLost(breedNameField, itemText);
}//GEN-LAST:event_breedNameFieldFocusLost

    private void breedNameFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_breedNameFieldKeyReleased
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
            jButton2ActionPerformed(null);
    }//GEN-LAST:event_breedNameFieldKeyReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField breedNameField;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables

}
